import java.util.List;

public class MySingleList {
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//永远指向头节点

    public void createList() {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        this.head = node1;
    }

    //遍历单链表
    public void show() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //从指定位置打印
    public void show2(ListNode newHead) {
        ListNode cur = newHead;
        while(cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //链表的长度
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //查找是否包含关键字key
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            //如果val是引用类型,则这里要用equals来比较！！！
            if(cur.val == key)
                return true;
            else
                cur = cur.next;
        }
        return false;
    }

    //头插
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;

    }

    //尾插
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            return;
        }

        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    //任意位置插入
    public void addIndex(int index, int data) {

        //判断index的合法性
        int len = size();
        if(index < 0 || index > len) {
            throw new IndexOutOfBounds("任意位置插入数据时，Index位置不合法！"+index);
        }

        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == len){
            addLast(data);
            return;
        }
        //index前一个位置
        ListNode cur = findIndex(index);
        //插入数据
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    //找到Index-1的位置
    private ListNode findIndex(int index) {
        ListNode cur = head;
        while(index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //删除第一次出现关键字key的结点
    public void remove(int key) {
        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            return;
        }

        ListNode prev = searchPrev(key);
        if(prev == null) {
            System.out.println("没有这个数据！");
            return;
        }
        ListNode del = prev.next;
        prev.next = del.next;
    }

    //找到要删除位置的前一个结点
    private ListNode searchPrev(int key) {
        ListNode prev = head;
        while(prev.next != null) {
            if(prev.next.val == key) {
                return prev;
            }else {
                prev = prev.next;
            }
        }
        return null;
    }

    //删除所有key，面试题，要求只遍历1遍（时间复杂度O(N)）
    public void removeAllKey(int key) {
        if (head == null) {
            return;
        }

        ListNode cur = head.next;
        ListNode prev = head;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }

        //处理头节点为key的情况（写在前面和下面都可以,写前面把if换成while就可以）
        if(head.val == key) {
            head = head.next;
        }
    }

    //清空所有结点
    public void clear() {
        //1.
        while(head != null) {
            ListNode headNext = head.next;
            head.next = null;
            head = headNext;
        }
        //2.
        //this.head = null;
    }

    //反转链表
    public ListNode reverseList() {
        if(head == null) {
            return null;
        }
        if(head.next == null) {
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    //链表的中间结点
    public ListNode middleNode() {
        ListNode slow = head;
        ListNode fast = head;
        //注意：fast!=null && fast.next!=null顺序不能反
        //这里必须写&&,假如此时fast等于最后一个结点，那么fast.next.next则回空指针异常
        while(fast!=null && fast.next!=null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

    //返回倒数第K个结点
    public ListNode findKthToTail(int k) {
        if(k <= 0 || k > size()) {
            return null;
        }

        ListNode fast = head;
        ListNode slow = head;

        while(k > 0) {
            fast = fast.next;
            k--;
        }

        while(fast != null) {
            fast = fast.next;
            slow = slow.next;
        }

        return slow;
    }



    //链表分割
    public ListNode partition(int x) {
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        ListNode cur = head;
        while(cur != null) {
            if(cur.val < x) {
                if(bs == null) {
                    bs = be = cur;
                    //cur = cur.next;省略1
                }else {
                 be.next = cur;
                 be = cur;
                 //cur = cur.next;省略1
                }
                //cur = cur.next;省略2
            }else {
                if(as == null) {
                    as = ae = cur;
                   // cur = cur.next;省略1
                }else {
                    ae.next = cur;
                    ae = cur;
                    //cur = cur.next;省略1
                }
               // cur = cur.next;省略2
            }
            cur = cur.next;
        }

        //考虑：2个段都一定有数据吗？
        //第一个段没有数据，直接返回第二个段
        //如果第二个段为空，直接返回空也没问题
        if(bs == null) {
            return as;
        }
        be.next = as;
        if(as != null) {
            ae.next = null;
        }
        return bs;
    }


    //链表的回文结构
    public boolean chkPalindrome(ListNode head) {
        if(head == null) {
            return false;
        }

        //1.找中间结点
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        //反转链表
        ListNode cur = slow.next;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //判断回文
        while(head != slow) {
            if(head.val != slow.val) {
                return false;
            }
            //针对偶数的判断
            if(head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }


}
